﻿/// Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤  16).

/// tank you http://forums.academy.telerik.com/52312/c%23-numeral-systems-7-%D0%B7%D0%B0%D0%B4%D0%B0%D1%87%D0%B0?show=54749#a54749
/// 
using System.Collections.Generic;
using System;

namespace c_sh_II_lek_7_numeral
{
    class zad6
    {
        static void Main()
        {

            Console.Write("From numeral system [2 .. 16]: ");
            int s = int.Parse(Console.ReadLine());
            Console.Write("To numeral system [2 .. 16]: ");
            int d = int.Parse(Console.ReadLine());
            Console.Write("Enter number: ");
            string number = (Console.ReadLine()).ToUpper();
            
            ConvertFromDec(ConvertToDec(number, s), d);
        }
        
        static int ConvertToDec(string number, int baseFrom)
        {
            int decNum = 0;
            for (int i = 0; i < number.Length; i++)
            {
                if (number[i] > '9')
                {
                    decNum += (number[i] - '7') * (int)Math.Pow(baseFrom, (number.Length - 1 - i));
                }
                else
                {
                    decNum += (number[i] - '0') * (int)Math.Pow(baseFrom, (number.Length - 1 - i));
                }
            }
            return decNum;
        }
        static void ConvertFromDec(int number, int baseTo)
        {
            List<int> result = new List<int>();
            if (baseTo > 10)
            {
                while (number > 0)
                {
                    result.Add(number % baseTo);
                    number = number / baseTo;
                }
                result.Reverse();
                foreach (var item in result)
                {
                    switch (item)
                    {
                        case 10: Console.Write("A");
                            break;
                        case 11: Console.Write("B");
                            break;
                        case 12: Console.Write("C");
                            break;
                        case 13: Console.Write("D");
                            break;
                        case 14: Console.Write("E");
                            break;
                        case 15: Console.Write("F");
                            break;
                        default: Console.Write(item);
                            break;
                    }
                }
                Console.WriteLine();
            }
            else
            {
                while (number > 0)
                {
                    result.Add(number % baseTo);
                    number = number / baseTo;
                }
                result.Reverse();
                foreach (var item in result)
                {
                    Console.Write(item);
                }
                Console.WriteLine();
            }
        }
        
    }
}
